package com.kaili.basic.service.impl;

/**
 * Created by xxx on 2019-03-07.
 */

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.basic.mapper.*;
import com.kaili.basic.pojo.dto.BusinessOperationDeleteDto;
import com.kaili.basic.pojo.dto.OutboundReturnAddDto;
import com.kaili.basic.pojo.dto.OutboundReturnProductDto;
import com.kaili.basic.pojo.dto.OutboundReturnQueryDto;
import com.kaili.basic.pojo.entity.*;
import com.kaili.basic.service.OutboundApplicationService;
import com.kaili.basic.service.OutboundReturnService;
import com.kaili.common.util.LoginDetailUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xxx
 * @Copyright: Copyright (c) 2017
 * @Company: 杭州凯立通信有限公司
 */
@Service
public class OutboundReturnServiceImpl implements OutboundReturnService {
    @Autowired
    OutboundReturnMapper outboundReturnMapper;
    @Autowired
    OutboundReturnProductMapper outboundReturnProductMapper;
    @Autowired
    SupplierMapper supplierMapper;
    @Autowired
    OutboundApplicationService outboundApplicationService;
    @Autowired
    OutboundProductMapper outboundProductMapper;
    @Autowired
    OutboundApplicationMapper outboundApplicationMapper;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int addOutboundReturn(OutboundReturnAddDto outboundReturnAddDto) {
        OutboundReturn outboundReturn = new OutboundReturn();
        BeanUtils.copyProperties(outboundReturnAddDto, outboundReturn);
        Long returnId = outboundReturnMapper.getMaxReturnId();
        returnId = returnId + 1L;
        outboundReturn.setReturnOdd(StringUtils.leftPad(returnId.toString(), 6, "0"));
        outboundReturn.setPreparedBy(LoginDetailUtils.getCurrent().getUsername());
        outboundReturn.setCreateBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
        outboundReturn.setReturnId(returnId);
        Supplier supplier=supplierMapper.querySupplierById(outboundReturn.getSupplierId());
        if(supplier!=null){
            outboundReturn.setSupplierCode(supplier.getSupplierCode());
            outboundReturn.setSupplierName(supplier.getSupplierName());
        }
        outboundReturnMapper.addOutboundReturn(outboundReturn);
        int order=1;
        for (OutboundReturnProductDto outboundReturnProductDto : outboundReturnAddDto.getOutboundReturnProductList()) {
            OutboundReturnProduct outboundReturnProduct=new OutboundReturnProduct();
            BeanUtils.copyProperties(outboundReturnProductDto, outboundReturnProduct);
            outboundReturnProduct.setReturnId(returnId);
            outboundReturnProduct.setCreateBy(outboundReturn.getCreateBy());
            outboundReturnProduct.setReturnOrder(order);
            outboundReturnProductMapper.addOutboundReturnProduct(outboundReturnProduct);
            order++;
        }
        //修改出库申请以及出库产品信息(数量，重量，体积等)
        outboundApplicationService.updateOrderOutboundReturn(outboundReturnAddDto.getOutboundReturnProductList(),outboundReturnAddDto.getRequestCode());
        return 1;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int deleteOutboundReturn(OutboundReturn outboundReturn) {
        outboundReturnMapper.deleteOutboundReturn(outboundReturn);
        OutboundReturnProduct outboundReturnProduct =new OutboundReturnProduct();
        outboundReturnProduct.setReturnId(outboundReturn.getReturnId());
        outboundReturnProduct.setModifyBy(outboundReturn.getModifyBy());
        outboundReturnProductMapper.deleteOutboundReturnProduct(outboundReturnProduct);
        return 1;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int deleteOutboundReturns(BusinessOperationDeleteDto deleteDto) {
        Long employeeId=LoginDetailUtils.getCurrentEmployeeId().longValue();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("modifyBy",employeeId);
        if(deleteDto.getMainIds()!=null&&deleteDto.getMainIds().length>0){
            map.put("returnIds",deleteDto.getMainIds());
            outboundReturnMapper.deleteOutboundReturns(map);
            for(Long returnId:deleteDto.getMainIds()) {
                Long applyId = 0L;
                List<OutboundReturnProduct> list = outboundReturnProductMapper.getOutboundReturnProductList(returnId);
                for (OutboundReturnProduct outboundReturnProductMy : list) {
                    //查询出库产品信息
                    OutboundProduct outboundProductMy = outboundProductMapper.getOutboundProductById(outboundReturnProductMy.getOutProductId());
                    //修改出库产品信息(数量，重量，体积等)
                    if (outboundProductMy != null) {
                        applyId = outboundProductMy.getApplyId();
                        OutboundProduct outboundProduct = new OutboundProduct();
                        outboundProduct.setId(outboundReturnProductMy.getOutProductId());
                        outboundProduct.setModifyBy(employeeId);
                        outboundProduct.setReturnQuantity(outboundProductMy.getReturnQuantity() + outboundReturnProductMy.getQuantity());
                        outboundProduct.setReturnWeight(outboundProductMy.getReturnWeight() + outboundReturnProductMy.getWeight());
                        outboundProduct.setReturnVolume(outboundProductMy.getReturnVolume() + outboundReturnProductMy.getVolume());
                        outboundProduct.setSalesReturnQuantity(outboundProductMy.getSalesReturnQuantity() - outboundReturnProductMy.getQuantity());
                        outboundProductMapper.updateOutboundProduct(outboundProduct);
                    }
                }
                //查询子项的主表下总出库数量和总数量的差来判断出库的状态
                OutboundApplication outboundApplication = new OutboundApplication();
                outboundApplication.setApplyId(applyId);
                outboundApplication.setModifyBy(employeeId);
                Long sumReturnQuantity = outboundApplicationMapper.getSumReturnQuantity(applyId);
                if (sumReturnQuantity == null) {
                    outboundApplication.setSalesReturnStatus(2);
                } else {
                    if (sumReturnQuantity.longValue() > 0L) {
                        outboundApplication.setSalesReturnStatus(1);
                    } else {
                        outboundApplication.setSalesReturnStatus(2);
                    }
                }
                outboundApplicationMapper.updateOutboundApplication(outboundApplication);
            }
        }
        if(deleteDto.getSonIds()!=null&&deleteDto.getSonIds().length>0){
            map.put("ids",deleteDto.getSonIds());
            outboundReturnProductMapper.deleteOutboundReturnProductList(map);
            for(Long id:deleteDto.getSonIds()) {
                OutboundReturnProduct outboundReturnProduct = outboundReturnProductMapper.getOutboundReturnProductByIdAll(id);
                //如果批量删除中有当前子项主表的id则不需要回退出库信息的数量，体积等信息，以免多回退数量，体积等
                OutboundReturn outboundReturn = outboundReturnMapper.queryOutboundReturnById(outboundReturnProduct.getReturnId());
                if (outboundReturn != null) {
                    Long applyId = 0L;
                    OutboundProduct outboundProductMy = outboundProductMapper.getOutboundProductById(outboundReturnProduct.getOutProductId());
                    if (outboundProductMy != null) {
                        applyId = outboundProductMy.getApplyId();
                        OutboundProduct outboundProduct = new OutboundProduct();
                        outboundProduct.setId(outboundReturnProduct.getOutProductId());
                        outboundProduct.setModifyBy(employeeId);
                        outboundProduct.setReturnQuantity(outboundProductMy.getReturnQuantity() + outboundReturnProduct.getQuantity());
                        outboundProduct.setReturnWeight(outboundProductMy.getReturnWeight() + outboundReturnProduct.getWeight());
                        outboundProduct.setReturnVolume(outboundProductMy.getReturnVolume() + outboundReturnProduct.getVolume());
                        outboundProduct.setSalesReturnQuantity(outboundProductMy.getSalesReturnQuantity() - outboundReturnProduct.getQuantity());
                        outboundProductMapper.updateOutboundProduct(outboundProduct);
                    }
                    //修改出库申请信息的退货状态，总的退货数量为0则没有退货
                    OutboundApplication outboundApplication = new OutboundApplication();
                    outboundApplication.setApplyId(applyId);
                    outboundApplication.setModifyBy(employeeId);
                    Long sumReturnQuantity = outboundApplicationMapper.getSumReturnQuantity(applyId);
                    if (sumReturnQuantity == null) {
                        outboundApplication.setSalesReturnStatus(2);
                    } else {
                        if (sumReturnQuantity.longValue() > 0L) {
                            outboundApplication.setSalesReturnStatus(1);
                        } else {
                            outboundApplication.setSalesReturnStatus(2);
                        }
                    }
                    outboundApplicationMapper.updateOutboundApplication(outboundApplication);
                }
            }
        }
        return 1;
    }

    @Override
    public Page<OutboundReturn> queryOutboundReturn(Page<OutboundReturn> page, OutboundReturnQueryDto outboundReturn) {
        List<OutboundReturn> outboundReturns = outboundReturnMapper.queryOutboundReturn(page,outboundReturn);
        return page.setRecords(outboundReturns);
    }

    @Override
    public OutboundReturn queryOutboundReturnById(Long returnId) {
        return outboundReturnMapper.queryOutboundReturnById(returnId);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int updateAffirmOrCancel(OutboundReturn outboundReturn) {
        OutboundReturn outboundReturnMy=outboundReturnMapper.queryOutboundReturnById(outboundReturn.getReturnId());
        if(outboundReturnMy!=null&&outboundReturnMy.getOrderStatus().intValue()==1) {
            outboundReturnMapper.updateOutboundReturn(outboundReturn);
            OutboundReturnProduct outboundReturnProduct = new OutboundReturnProduct();
            outboundReturnProduct.setReturnId(outboundReturn.getReturnId());
            outboundReturnProduct.setModifyBy(outboundReturn.getModifyBy());
            outboundReturnProduct.setOrderStatus(outboundReturn.getOrderStatus());
            outboundReturnProductMapper.updateConfirmToCancel(outboundReturnProduct);
            Long applyId = 0L;
            List<OutboundReturnProduct> list = outboundReturnProductMapper.getOutboundReturnProductList(outboundReturn.getReturnId());
            for (OutboundReturnProduct outboundReturnProductMy : list) {
                OutboundProduct outboundProductMy = outboundProductMapper.getOutboundProductById(outboundReturnProductMy.getOutProductId());
                if (outboundProductMy != null) {
                    applyId = outboundProductMy.getApplyId();
                    OutboundProduct outboundProduct = new OutboundProduct();
                    outboundProduct.setId(outboundReturnProductMy.getOutProductId());
                    outboundProduct.setModifyBy(outboundReturn.getModifyBy());
                    if (outboundReturn.getOrderStatus().intValue() == 2) {
                        //订单确认的操作
                        outboundProduct.setSalesReturnQuantity(outboundProductMy.getSalesReturnQuantity() - outboundReturnProductMy.getQuantity());
                        outboundProduct.setActualQuantityReturned(outboundProductMy.getActualQuantityReturned() + outboundReturnProductMy.getQuantity());
                    } else {
                        //订单取消的操作，回退数量，体积等
                        outboundProduct.setReturnQuantity(outboundProductMy.getReturnQuantity() + outboundReturnProductMy.getQuantity());
                        outboundProduct.setReturnWeight(outboundProductMy.getReturnWeight() + outboundReturnProductMy.getWeight());
                        outboundProduct.setReturnVolume(outboundProductMy.getReturnVolume() + outboundReturnProductMy.getVolume());
                        outboundProduct.setSalesReturnQuantity(outboundProductMy.getSalesReturnQuantity() - outboundReturnProductMy.getQuantity());
                    }
                    outboundProductMapper.updateOutboundProduct(outboundProduct);
                }
            }
            //修改出库申请信息的退货状态，总的退货数量为0则没有退货
            OutboundApplication outboundApplication = new OutboundApplication();
            outboundApplication.setApplyId(applyId);
            outboundApplication.setModifyBy(outboundReturn.getModifyBy());
            Long sumReturnQuantity = outboundApplicationMapper.getSumReturnQuantity(applyId);
            if (sumReturnQuantity == null) {
                outboundApplication.setSalesReturnStatus(2);
            } else {
                if (sumReturnQuantity.longValue() > 0L) {
                    outboundApplication.setSalesReturnStatus(1);
                } else {
                    outboundApplication.setSalesReturnStatus(2);
                }
            }
            outboundApplicationMapper.updateOutboundApplication(outboundApplication);
        }
        return 1;
    }

    @Override
    public Long getMaxReturnId() {
        return outboundReturnMapper.getMaxReturnId();
    }

    @Override
    public List<OutboundReturnExport> queryOutboundReturnExport(OutboundReturnQueryDto outboundReturnQueryDto) {
        return outboundReturnMapper.queryOutboundReturnExport(outboundReturnQueryDto);
    }
}
